package com.gc.server.controller;

import java.util.List;

import com.gc.server.entity.ApplyItemEntity;
import com.gc.server.service.ApplyItemService;
import com.gc.server.vo.GoodsApplyVo;
import com.yf.request.SearchParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import com.gc.server.entity.GoodsApplyEntity;
import com.gc.server.service.GoodsApplyService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 物料申请Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Mar 10 11:17:45 CST 2020
 */
@RestController
@RequestMapping("/goods/apply")
public class GoodsApplyController {
    @Autowired
    private GoodsApplyService goodsApplyService;

    @Autowired
    private ApplyItemService applyItemService;

    /**
     * 列表物料申请
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:apply:list")
    public R list(@RequestBody SearchParam<GoodsApplyVo> params) {
        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<GoodsApplyVo> goodsApplyList = goodsApplyService.queryListVo(params.getEntity());
        return R.ok(new PageInfo(goodsApplyList));
    }

    /**
     * 查看物料申请
     */
    @SysLog("查看物料申请")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("goods:apply:info")
    public R info(@PathVariable("id") Long id) {
        GoodsApplyEntity goodsApply = goodsApplyService.get(id);
        List<ApplyItemEntity> items = applyItemService.findByApplyId(id);
        return R.ok(goodsApply).putExtra("items",items);
    }

    /**
     * 保存物料申请
     */
    @SysLog("保存物料申请")
    @RequestMapping("/save")
    @RequiresPermissions("goods:apply:save")
    public R save(GoodsApplyEntity goodsApply) {
        goodsApplyService.save(goodsApply);

        return R.ok(goodsApply);
    }

    /**
     * 修改物料申请
     */
    @SysLog("修改物料申请")
    @RequestMapping("/update")
    @RequiresPermissions("goods:apply:update")
    public R update(GoodsApplyEntity goodsApply) {
        goodsApplyService.update(goodsApply);

        return R.ok(goodsApply);
    }

    /**
     * 下载
     */
    @SysLog("下载")
    @RequestMapping("/down")
    @RequiresPermissions("goods:apply:list")
    public void down(@RequestBody SearchParam<GoodsApplyVo> params, HttpServletResponse response) {
        List<GoodsApplyVo> list = goodsApplyService.queryListVo(params.getEntity());
        goodsApplyService.downLoad(response, list);
    }

    /**
     * 删除物料申请
     */
    @SysLog("删除物料申请")
    @RequestMapping("/delete")
    @RequiresPermissions("goods:apply:delete")
    public R delete(Long[]ids) {
        goodsApplyService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有物料申请
     */
    @SysLog("所有物料申请")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<GoodsApplyEntity> goodsApplyList = goodsApplyService.queryList(params);

        return R.ok(goodsApplyList);
    }

    /**
    @ModelAttribute
    public SearchParam<GoodsApplyEntity> searchParam(SearchParam<GoodsApplyEntity> params, GoodsApplyEntity goodsApply) {
        if (goodsApply != null) {
            params.setEntity(goodsApply);
        }

        return params;
    }
 **/
    /***********自定义开始***********/
}
